{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d6365f9e-8c67-4944-bdae-39cea4c9b63f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！我是Qwen，是阿里巴巴集团旗下的通义实验室自主研发的超大规模语言模型。我可以帮助你回答问题、创作文字，比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等，还能表达观点，玩游戏等。我支持多种语言，包括但不限于中文、英文、德语、法语、西班牙语等。如果你有任何问题或需要帮助，随时告诉我！\n"
     ]
    }
   ],
   "source": [
    "#使用openai的官方sdk\n",
    "# import openai\n",
    "from openai import OpenAI  # 新版导入方式\n",
    "import os\n",
    "\n",
    "client = OpenAI(\n",
    "\tapi_key=\"sk-4e88cf4db3e14894bafaff606d296610\",\n",
    "\tbase_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    ")\n",
    "\n",
    "messages = [\n",
    "{\"role\": \"user\", \"content\": \"介绍下你自己\"}\n",
    "]\n",
    "\n",
    "res = client.chat.completions.create(\n",
    "    model=\"qwen-plus\",\n",
    "    messages=messages,\n",
    "    stream=False,\n",
    ")\n",
    "# print(res['choices'][0]['message']['content'])  # 打印“TypeError: 'ChatCompletion' object is not subscriptable”\n",
    "print(res.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "15e80ae9-f116-443f-918f-835a3d2c8d1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='带我飞往月球是我最喜欢的歌曲之一。'), Document(metadata={}, page_content='带我飞往月球是我最喜欢的歌曲之一。')]\n",
      "根据论文的方案对检索结果进行重新排序,返回高相关性内容块：\n",
      "[Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='带我飞往月球是我最喜欢的歌曲之一。'), Document(metadata={}, page_content='带我飞往月球是我最喜欢的歌曲之一。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='我非常喜欢去看电影。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。'), Document(metadata={}, page_content='凯尔特人队是我最喜欢的球队。')]\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains import LLMChain,StuffDocumentsChain\n",
    "from langchain.document_transformers import (\n",
    "    LongContextReorder\n",
    ")\n",
    "from langchain.embeddings import HuggingFaceBgeEmbeddings\n",
    "from langchain.vectorstores import  Chroma\n",
    "\n",
    "#使用huggingface托管的开源LLM来做嵌入，MiniLM-L6-v2是一个较小的LLM \n",
    "embedings = HuggingFaceBgeEmbeddings(model_name=\"D:/ideaSpace/MyPython/models/all-MiniLM-L6-v2\")\n",
    "\n",
    "text = [\n",
    "    \"篮球是一项伟大的运动。\",\n",
    "    \"带我飞往月球是我最喜欢的歌曲之一。\",\n",
    "    \"凯尔特人队是我最喜欢的球队。\",\n",
    "    \"这是一篇关于波士顿凯尔特人的文件。\",\n",
    "    \"我非常喜欢去看电影。\",\n",
    "    \"波士顿凯尔特人队以20分的优势赢得了比赛。\",\n",
    "    \"这只是一段随机的文字。\",\n",
    "    \"《艾尔登之环》是过去15年最好的游戏之一。\",\n",
    "    \"L.科内特是凯尔特人队最好的球员之一。\",\n",
    "    \"拉里.伯德是一位标志性的NBA球员。\"\n",
    "]\n",
    "\n",
    "# k：表示返回的最相似的文档数量。在这个例子中，k=10 表示检索器将返回与查询向量最相似的 10 个文档\n",
    "retrieval = Chroma.from_texts(text,embedings).as_retriever(\n",
    "    search_kwargs={\"k\": 10}\n",
    ")\n",
    "query = \"关于我的喜好都知道什么?\"\n",
    "\n",
    "#根据相关性返回文本块\n",
    "docs = retrieval.get_relevant_documents(query)\n",
    "print(docs)\n",
    "\n",
    "#对检索结果进行重新排序，根据论文的方案\n",
    "#问题相关性越低的内容块放在中间\n",
    "#问题相关性越高的内容块放在头尾\n",
    "reordering = LongContextReorder()\n",
    "reo_docs = reordering.transform_documents(docs)\n",
    "#头尾共有4个高相关性内容块\n",
    "print(\"根据论文的方案对检索结果进行重新排序,返回高相关性内容块：\")\n",
    "print(reo_docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5efa6924-36b9-4730-bbad-8c106a42e097",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\27727\\AppData\\Local\\Temp\\ipykernel_7576\\296177587.py:39: LangChainDeprecationWarning: This class is deprecated. Use the `create_stuff_documents_chain` constructor instead. See migration guide here: https://python.langchain.com/docs/versions/migrating_chains/stuff_docs_chain/\n",
      "  WorkChain = StuffDocumentsChain(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'根据提供的文本内容，你最喜欢的活动是：**看电影**。\\n\\n虽然你喜欢凯尔特人队和歌曲“带我飞往月球”，但提到次数最多的是“我非常喜欢去看电影”，共出现了 **4次**，而“凯尔特人队是我最喜欢的球队”出现了 **2次**，“带我飞往月球是我最喜欢的歌曲之一”出现了 **2次**。\\n\\n因此，从频率来看，**看电影** 是你最喜欢的活动。'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#检测下这种方案的精度效果\n",
    "from langchain.prompts import PromptTemplate\n",
    "# from langchain.llms import OpenAI\n",
    "from langchain_community.chat_models import ChatOpenAI\n",
    "\n",
    "api_base = \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n",
    "api_key = \"sk-4e88cf4db3e14894bafaff606d296610\"\n",
    "\n",
    "#设置llm\n",
    "llm = ChatOpenAI(\n",
    "    openai_api_key=api_key,\n",
    "    openai_api_base=api_base,\n",
    "    model=\"qwen-plus\",  # 使用通义千问 plus 模型\n",
    "    temperature=0,      # 温度为0，确保输出确定性\n",
    ")\n",
    "\n",
    "document_prompt = PromptTemplate(\n",
    "    input_variables=[\"page_content\"],template=\"{page_content}\"\n",
    ")\n",
    "\n",
    "stuff_prompt_override =\"\"\"Given this text extracts:\n",
    "----------------------------------------\n",
    "{context}\n",
    "----------------------------------------\n",
    "Please answer the following questions:\n",
    "{query}\n",
    "\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=stuff_prompt_override,\n",
    "    input_variables=[\"context\",\"query\"]\n",
    ")\n",
    "\n",
    "llm_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=prompt\n",
    ")\n",
    "\n",
    "WorkChain = StuffDocumentsChain(\n",
    "    llm_chain=llm_chain,\n",
    "    document_prompt=document_prompt,\n",
    "    document_variable_name=\"context\"\n",
    ")\n",
    "\n",
    "#调用\n",
    "WorkChain.run(\n",
    "    input_documents=reo_docs,\n",
    "    query=\"我最喜欢做什么事情？\"\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6704c7b6-5c42-4edb-9b98-6c45494b1fce",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rag_learn",
   "language": "python",
   "name": "rag_learn"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
